package springboot.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springboot.bean.Stack;
import springboot.bean.StackExample;
import springboot.bean.Status;
import springboot.dao.StackDAO;
import springboot.websocket.WebSocketServer;

import java.io.IOException;
import java.util.List;

@RestController
@Slf4j
public class ApplicationController {

    byte[] C=new byte[8];
    byte[] D1=new byte[8];
    byte[] D2=new byte[8];
    StackExample ex=null;
    StackExample.Criteria criteria=null;
    Stack stack=null;
    List<Stack> list=null;
    Stack stack1=null;
    Stack stack2=null;
    Stack stack3=null;
    Stack stack4=null;

    Stack stack01=null;  Stack stack02=null;
    Stack stack03=null;  Stack stack04=null;
    Stack stack05=null;  Stack stack06=null;
    Stack stack07=null;  Stack stack08=null;
    Stack stack09=null;  Stack stack10=null;
    @Autowired
    private StackDAO stackDAO;

    /**
     * 参数,所有传感器的状态
     * @param status
     * 功能：将传感器的货位状态实时传送到数据库中,但是正在使用中的货位不要更新
     */
    @PostMapping("/updateStack")
    public void showStackStatus(@RequestBody Status status) throws IOException {
        C=status.getByteFromPLc6();
        D1=status.getByteFromPLc7();
        D2=status.getByteFromPLc8();
        /*对比一下，如果不一样则更新*/
        ex =new StackExample();
        criteria =ex.createCriteria();
        criteria.andStatusNotEqualTo("TOUSE");
        list=stackDAO.selectByExample(ex);
        /*没有任务那么更新所有货位*/
        if (list.size()==14){
            for (int i=0;i<4;i++){
                ex =new StackExample();
                criteria=ex.createCriteria();
                criteria.andIndexEqualTo(String.valueOf(i));
                criteria.andLineBetween("2","3");
                stack=new Stack();
                if (C[i]==0){
                    stack.setStatus("NO");
                }else if (C[i]==1){
                    stack.setStatus("YES");
                }
                stackDAO.updateByExampleSelective(stack,ex);
            }
            for (int i=0;i<8;i++){
                ex =new StackExample();
                criteria=ex.createCriteria();
                criteria.andIndexEqualTo(String.valueOf(i));
                criteria.andLineBetween("4","5");
                stack=new Stack();
                if (D1[i]==0){
                    stack.setStatus("NO");
                }else if (D1[i]==1){
                    stack.setStatus("YES");
                }
                stackDAO.updateByExampleSelective(stack,ex);
            }
            for (int i=0;i<2;i++){
                ex =new StackExample();
                criteria=ex.createCriteria();
                criteria.andIndexEqualTo(String.valueOf(i));
                criteria.andLineBetween("6","7");
                stack=new Stack();
                if (D2[i]==0){
                    stack.setStatus("NO");
                }else if (D2[i]==1){
                    stack.setStatus("YES");
                }
                stackDAO.updateByExampleSelective(stack,ex);
            }
        }else{
            for (int i=0;i<4;i++){
                stack=stackDAO.selectByPrimaryKey(i+1);
                if (stack!=null && stack.getStatus().equals("TOUSE")){

                }else {
                    stack=new Stack();
                    if (C[i]==0){
                        stack.setStatus("NO");
                    }else if (C[i]==1){
                        stack.setStatus("YES");
                    }
                    ex =new StackExample();
                    criteria=ex.createCriteria();
                    criteria.andIndexEqualTo(String.valueOf(i));
                    criteria.andLineBetween("2","3");
                    stackDAO.updateByExampleSelective(stack,ex);
                }
            }
            for (int i=0;i<8;i++){
                stack=stackDAO.selectByPrimaryKey(i+5);
                if (stack!=null && stack.getStatus().equals("TOUSE")){
                }else {
                    stack=new Stack();
                    if (D1[i]==0){
                        stack.setStatus("NO");
                    }else if (D1[i]==1){
                        stack.setStatus("YES");
                    }
                    ex =new StackExample();
                    criteria=ex.createCriteria();
                    criteria.andIndexEqualTo(String.valueOf(i));
                    criteria.andLineBetween("4","5");
                    stackDAO.updateByExampleSelective(stack,ex);
                }
            }
            for (int i=0;i<2;i++){
                stack=stackDAO.selectByPrimaryKey(i+13);
                if (stack!=null && stack.getStatus().equals("TOUSE")){
                }else {
                    stack=new Stack();
                    if (D2[i]==0){
                        stack.setStatus("NO");
                    }else if (D2[i]==1){
                        stack.setStatus("YES");
                    }
                    ex =new StackExample();
                    criteria=ex.createCriteria();
                    criteria.andIndexEqualTo(String.valueOf(i));
                    criteria.andLineBetween("6","7");
                    stackDAO.updateByExampleSelective(stack,ex);
                }
            }
        }
        /*在线 就查询并发送信息*/
        if (WebSocketServer.IS_ONLINE){
            ex =new StackExample();
            list=stackDAO.selectByExample(ex);
            WebSocketServer.sendInfo(list);
        }
    }
    @GetMapping("/getStackToC")
    public String getStackPositionToC(){
        stack1=stackDAO.selectByPrimaryKey(1);
        stack2=stackDAO.selectByPrimaryKey(2);
        stack3=stackDAO.selectByPrimaryKey(3);
        stack4=stackDAO.selectByPrimaryKey(4);
        if (stack1.getStatus().equals("NO")&&stack2.getStatus().equals("NO")){
            return "1008";
        }else if (stack3.getStatus().equals("NO")&&stack4.getStatus().equals("NO")){
            return "1010";
        }else if (stack2.getStatus().equals("NO")){
            return "1009";
        }else if (stack4.getStatus().equals("NO")){
            return "1011";
        }
        return "NO";
    }
    @GetMapping("/getStackToD")
    public String getStackPositionToD(){
        stack01=stackDAO.selectByPrimaryKey(5);
        stack02=stackDAO.selectByPrimaryKey(6);
        stack03=stackDAO.selectByPrimaryKey(7);
        stack04=stackDAO.selectByPrimaryKey(8);
        stack05=stackDAO.selectByPrimaryKey(9);
        stack06=stackDAO.selectByPrimaryKey(10);
        stack07=stackDAO.selectByPrimaryKey(11);
        stack08=stackDAO.selectByPrimaryKey(12);
        stack09=stackDAO.selectByPrimaryKey(13);
        stack10=stackDAO.selectByPrimaryKey(14);
        if (stack01.getStatus().equals("NO")&&stack03.getStatus().equals("NO")&&
            stack05.getStatus().equals("NO")&&stack07.getStatus().equals("NO")&&
            stack09.getStatus().equals("NO")){
            return "1030";
        }else if (stack02.getStatus().equals("NO")&&stack04.getStatus().equals("NO")&&
                stack06.getStatus().equals("NO")&&stack08.getStatus().equals("NO")&&
                stack10.getStatus().equals("NO")){
            return "1031";
        }else if (stack03.getStatus().equals("NO")&& stack05.getStatus().equals("NO")&&
                stack07.getStatus().equals("NO")&& stack09.getStatus().equals("NO")){
            return "1032";
        }else if (stack04.getStatus().equals("NO")&& stack06.getStatus().equals("NO")&&
                stack08.getStatus().equals("NO")&& stack10.getStatus().equals("NO")){
            return "1033";
        }else if (stack05.getStatus().equals("NO")&& stack07.getStatus().equals("NO")&&
                stack09.getStatus().equals("NO")){
            return "1034";
        }else if (stack06.getStatus().equals("NO")&& stack08.getStatus().equals("NO")&&
                stack10.getStatus().equals("NO")){
            return "1035";
        }else if (stack07.getStatus().equals("NO")&& stack09.getStatus().equals("NO")){
            return "1036";
        }else if (stack08.getStatus().equals("NO")&& stack10.getStatus().equals("NO")){
            return "1037";
        }else if (stack09.getStatus().equals("NO")){
            return "1038";
        }else if (stack10.getStatus().equals("NO")){
            return "1039";
        }
        return "NO";
    }


    StackExample example2=null;
    StackExample.Criteria criteria2=null;
    Stack stack11=null;
    @GetMapping("/updateStackByEnd/{position}")
    public void updateStackByEnd(@PathVariable String position){
        example2=new StackExample();
        criteria2=example2.createCriteria();
        criteria2.andPositionEqualTo(position);
        stack11=new Stack();
        stack11.setStatus("TOUSE");
        stackDAO.updateByExampleSelective(stack11,example2);
    }

    StackExample example=null;
    StackExample.Criteria criteria1=null;
    Stack stack00=null;
    @GetMapping("/updateToYES/{position}")
    public void updateToYES(@PathVariable String position){
        example=new StackExample();
        criteria1=example.createCriteria();
        criteria1.andPositionEqualTo(position);
        stack00=new Stack();
        stack00.setStatus("YES");
        stackDAO.updateByExampleSelective(stack00,example);
    }

    @GetMapping("/forbid")
    public void forbidStack(int index){
        StackExample stackExample=new StackExample();
        StackExample.Criteria criteriaForbid=stackExample.createCriteria();
        criteriaForbid.andIdEqualTo(index);
        Stack stackForbid=new Stack();
        stackForbid.setStatus("NOUSE");
        stackDAO.updateByExampleSelective(stackForbid,stackExample);
    }

    @GetMapping("/clear")
    public void clearStack(int index){
        StackExample stackExample=new StackExample();
        StackExample.Criteria criteriaForbid=stackExample.createCriteria();
        criteriaForbid.andIdEqualTo(index);
        Stack stackForbid=new Stack();
        stackForbid.setStatus("NO");
        stackDAO.updateByExampleSelective(stackForbid,stackExample);
    }
}
